var express = require('express');
const { default: mongoose } = require('mongoose');
var router = express.Router();
const { planModel, baogaoModel, zhouplanModel, riplanModel, yueplanModel,
	jiplanModel, bannianplanModel, gongzuonianplanModel,
	zhijieplanModel, gongzuoplanModel, xiashuplanModel, 
	ri_reportModel, zhou_reportModel, yue_reportModel, ji_reportModel } = require('../module/mry')
router.get('/region', async (req, res) => {
	res.send({})
})
router.get('/plans', async (req, res) => {
	let riplan = await riplanModel.find({ status: '日计划' }).exec()
	let zhouplan = await zhouplanModel.find({ status: '周计划' }).exec()
	let yueplan = await yueplanModel.find({ status: '月计划' }).exec()
	let jiplan = await jiplanModel.find({ status: '半年计划' }).exec()
	let bannianplan = await bannianplanModel.find({ status: '半年工作计划' }).exec()
	let gongzuonianplan = await gongzuonianplanModel.find({ status: '工作年计划' }).exec()
	let zhijieplan = await zhijieplanModel.find({ status: '直接计划展示' }).exec()
	let gongzuoplan = await gongzuoplanModel.find({ status: '所有工作计划' }).exec()
	let xiashuplan = await xiashuplanModel.find({ status: '工作下属计划' }).exec()
	res.send({ riplan, zhouplan, yueplan, jiplan, bannianplan, gongzuonianplan, zhijieplan, gongzuoplan, xiashuplan })
})
// 报告
router.get('/getreport', async (req, res) => {
	let data;
	let title = req.query.title
	let faqiren = req.query.faqiren
	let faqirenreg = new RegExp(faqiren, 'i')
	let titlereg = new RegExp(title, 'i')
	let searcharr = [];
	if (title) {
		searcharr.push({ title: titlereg })
	}
	if (faqiren) {
		searcharr.push({ faqiren: faqirenreg })
	}
	if (searcharr.length > 0) {
		data = await baogaoModel.find({ $and: searcharr })
	}
	else {
		data = await baogaoModel.find()
	}
	res.send({
		data
	})
})
//新增报告
router.post('/addbaogao', async (req, res) => {
	let body = req.body;
	await baogaoModel.create(body);
	res.send({
		msg: '新增成功'
	})
})

// 删除报告
router.delete('/delbaogao/:id', async (req, res) => {
	await baogaoModel.deleteOne({ _id: req.params.id })
	res.send({
		msg: '删除成功'
	})
})

// 
// 获取计划
router.get('/getplans', async (req, res) => {
	let data;
	let anutitle = req.query.anutitle
	let anutype = req.query.anutype
	let anustate = req.query.anustate
	let anutitlereg = new RegExp(anutitle, 'i')
	let searcharr = [];
	if (anutitle) {
		searcharr.push({ anutitle: anutitlereg })
	}
	if (anutype) {
		searcharr.push({ anutype })
	}
	if (anustate) {
		searcharr.push({ anustate })
	}
	if (searcharr.length > 0) {
		data = await planModel.find({ $and: searcharr })
	}
	else {
		data = await planModel.find()
	}
	res.send({
		data
	})
})

// 删除计划
router.delete('/delplan/:id', async (req, res) => {
	console.log(req.params.id);
	await planModel.deleteOne({ _id: req.params.id })
	res.send({
		msg: '删除成功'
	})
})

// 新增计划
router.post('/addplan', async (req, res) => {
	let body = req.body
	await planModel.create(body)
	res.send({
		msg: '新增成功'
	})
})

module.exports = router;